﻿using System;
using System.IO;
using AgFx;
using DepSoft.Mobile.WP7.FollwIt.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    /// <summary>
    /// We cache a Movie Summary for 1 month
    /// We do a cache then refresh, because this will give us following principle
    /// 1. App will make request, [A, B, C] will be returned from cache and displayed
    /// 2. AgFx will see that the cache is no longer valid (more than 3600 seconds have elapsed) and will kick off a new request
    /// 3. Server will return, say [C, D, E]
    /// 4. Cache will be replaced and UI will bind to new value
    /// </summary>

    [CachePolicy(CachePolicy.CacheThenRefresh, 3600 * 24 * 31)]
    public class agfxMovieSummaryViewModel : ModelItemBase<MovieSummaryLoadContext>
    {
        #region Public bindable properties
        /// <summary>
        /// The <see cref="MovieDetail" /> property's name.
        /// </summary>
        public const string MovieDetailPropertyName = "MovieDetail";

        private MovieSummary _movieDetail = null;

        /// <summary>
        /// Gets the MovieDetail property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public MovieSummary MovieDetail
        {
            get
            {
                return _movieDetail;
            }

            set
            {
                if (_movieDetail == value)
                {
                    return;
                }

                var oldValue = _movieDetail;
                _movieDetail = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(MovieDetailPropertyName);
            }
        }
        #endregion

        #region Constructor
        public agfxMovieSummaryViewModel()
        {

        }

        public agfxMovieSummaryViewModel(int movieID)
            : base(new MovieSummaryLoadContext(movieID))
        {

        }
        #endregion

        #region AgFx DataLoader
        public class agfxMovieSummaryViewModelLoader : IDataLoader<MovieSummaryLoadContext>
        {
            #region IDataLoader implementation
            public object Deserialize(MovieSummaryLoadContext loadContext, Type objectType, Stream stream)
            {
                var sr = new StreamReader(stream);
                var movieSummaryJSON = sr.ReadToEnd();
                JObject movieSummaryParsed = JObject.Parse(movieSummaryJSON);

                agfxMovieSummaryViewModel viewModel = new agfxMovieSummaryViewModel(loadContext.MovieID);

                JsonSerializer serializer = new JsonSerializer();
                MovieSummary movieSummary = (MovieSummary)serializer.Deserialize(new JTokenReader(movieSummaryParsed), typeof(MovieSummary));

                viewModel.MovieDetail = movieSummary;

                return viewModel;
            }

            public LoadRequest GetLoadRequest(MovieSummaryLoadContext loadContext, Type objectType)
            {
                string movieSummaryUriBase = FollwItApi.GetInstance().GetActionRestURI(RestAction.GetMovieSummary).Replace("movie_id", string.Format("movie_id/{0}", loadContext.MovieID.ToString()));
                return new WebLoadRequest(loadContext, new Uri(movieSummaryUriBase));
            }
            #endregion
        }
        #endregion
    }
}
